package com.wc.codeforces.模拟.Matrix_Stabilization;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/7/3 15:17
 * @description https://codeforces.com/problemset/problem/1986/B
 */
public class Main {
    /**
     * 思路：
     * 题目已经限制死了, 如果多个满足条件的从最小的开始变, 那我们直接从最小的开始枚举就可以了
     * aij = aij - 1, 那如果这个数还是满足, 很容易想到可以最多变到相邻的最大值就好了
     * 注意：为什么前面的可以直接变, 就不怕后面的变了嘛, 前面变成这个相等的,后面如果是最大的, 它就变不了了
     * 还有一点, 记得考虑边界问题
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 110;
    static int[][] a = new int[N][N];
    static int n, m;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            solve();
        }
        out.flush();
    }

    static void solve() {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int mx = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                mx = 0;
                if (i - 1 >= 1) mx = Math.max(a[i - 1][j], mx);
                if (i + 1 <= n) mx = Math.max(a[i + 1][j], mx);
                if (j - 1 >= 1) mx = Math.max(mx, a[i][j - 1]);
                if (j + 1 <= m) mx = Math.max(mx, a[i][j + 1]);
                if (mx < a[i][j]) a[i][j] = mx;
                out.print(a[i][j] + " ");
            }
            out.println();
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
